<?php
/**
 * 字符串操作
 *
 * Created	: 2012-06-21
 * Modified	: 2012-06-21
 * @version		: 0.1.0
 * @author	: Joseph Chen (chenliq@gmail.com)
 */
class String
{
	const RAND_INT		= 1;
	const RAND_LOWER	= 2;
	const RAND_UPPER	= 4;
	const RAND_SYMBOL	= 8;
	
	/**
	 * 判断变量是否纯数字ID的组合字符串(数字字符串用","分隔)
	 * @param int | string $s
	 * @return Boolean
	 */
	public static function digitSepBunch($s)
	{
		return (preg_match('/^(?:[\d]+\,{0,1})+[\d]+$/', $s));
	}
	
	/**
	 * 随机一个字符串
	 * @param int $len
	 * @param int $type
	 * @param string $exceptChars 需要排除的字符
	 */
	public static function rand($len=8, $type=7, $exceptChars='01iIlLoO')
	{
		$numeric = '0123456789';
		$lower	= 'abcdefghijklmnopqrstuvwxyz';
		$upper	= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$symbol  = '`~!@#$%^&*()-_=+[{]}/\\|;:\'",<.>/?';
		$ret = $str = '';
		($type & self::RAND_INT) && $str .= $numeric;
		($type & self::RAND_LOWER) && $str .= $lower;
		($type & self::RAND_UPPER) && $str .= $upper;
		($type & self::RAND_SYMBOL) && $str .= $symbol;
		for($i=0; $i<$len; $i++)
		{
			$rnd = mt_rand(0, strlen($str)-1);
			$char = $str[$rnd];
			if (false !== strpos($exceptChars, $char))
			{
				$i--;
				continue;
			}
			$ret .= $char;
		}
		return $ret;
	}
	
}